package com.ruicar.afs.cloud.gateway.handler;

import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.gateway.config.RsaConfiguration;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * The type SystemInitParamHandler.
 * <p>
 * Created on 2019-02-18
 *
 * @author Fzero
 */
@Slf4j
@Component
@AllArgsConstructor
public class SystemRsaPublicKeyHandler implements HandlerFunction<ServerResponse> {

	private final RsaConfiguration rsaConfiguration;

	/**
	 * Handle the given request.
	 *
	 * @param request the request to handle
	 * @return the response
	 */
	@Override
	public Mono<ServerResponse> handle(ServerRequest request) {
		return ServerResponse
				.status(HttpStatus.OK)
				.contentType(MediaType.APPLICATION_JSON)
				.body(BodyInserters.fromObject(
						IResponse.success(RsaPublic.builder().pubkey(rsaConfiguration.getRsaPublic()).build())
				));
	}

	@Data
	@Builder
	public static class RsaPublic {
		String pubkey;
	}
}
